package com.skyvin.nreceipts.admin.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.skyvin.nreceipts.admin.mongorepository.ReceiptCategoryRepository;
import com.skyvin.nreceipts.admin.mongorepository.ReceiptCategoryTemplateRepository;
import com.skyvin.nreceipts.common.document.AppUser;
import com.skyvin.nreceipts.common.document.ReceiptCategory;
import com.skyvin.nreceipts.common.document.ReceiptCategoryTemplate;
import com.skyvin.nreceipts.common.document.Retailer;
import com.skyvin.nreceipts.common.service.BaseService;

@Service
public class ReceiptCategoryService extends BaseService {
	
	Logger logger = Logger.getLogger(ReceiptCategoryService.class);

	@Autowired
	private ReceiptCategoryRepository receiptCategoryRepository; 
	
	@Autowired
	private ReceiptCategoryTemplateRepository receiptCategoryTemplateRepository; 
	
	@Autowired
	private MongoTemplate mongoTemplate;

	public void clonefromReceiptCategoryTemplate(AppUser appUserUpdated){
		List<ReceiptCategoryTemplate> receiptCategoryTemplates = receiptCategoryTemplateRepository.findAll();
		for (ReceiptCategoryTemplate receiptCategoryTemplate : receiptCategoryTemplates) {
			ReceiptCategory receiptCategory = new ReceiptCategory();
			receiptCategory.setId(null);
			receiptCategory.setValue(receiptCategoryTemplate.getValue());
			receiptCategory.setUserPID(appUserUpdated.getId());
			mongoTemplate.save(receiptCategory, getCollection(appUserUpdated.getId(), ReceiptCategory.class));
			receiptCategoryRepository.save(receiptCategory);
		}
		
	}

	@Transactional
	public ReceiptCategory save(ReceiptCategory receiptCategory) {
		receiptCategoryRepository.save(receiptCategory);
		mongoTemplate.save(receiptCategory, getMyCollection(ReceiptCategory.class));
		ReceiptCategory myreceiptCategoryCreated = mongoTemplate.findOne(new Query(Criteria.where("userPID").is(receiptCategory.getUserPID()).and("value").is(receiptCategory.getValue())), ReceiptCategory.class, getMyCollection(ReceiptCategory.class));
		logger.info("ID Generated : " + myreceiptCategoryCreated.getId());
		return myreceiptCategoryCreated;
	}
	@Transactional
	public void deleteMyCategory(String receiptCategoryPID) {
		//receiptCategoryRepository.delete(receiptCategoryPID);
		
		Query query = new Query(Criteria.where("id").is(receiptCategoryPID));
		mongoTemplate.findAndRemove(query, ReceiptCategory.class, getMyCollection(ReceiptCategory.class));
	}

	public List<ReceiptCategory> searchReceiptCategriesByName(String searchTerm, String currentUserPID) {
		Query query = new Query(Criteria.where("value").regex("(?i)"+searchTerm));
		List<ReceiptCategory> receiptCategories = mongoTemplate.find(query, ReceiptCategory.class, getMyCollection(ReceiptCategory.class));
		if(receiptCategories != null) {
			logger.info("No. of receipt categories found : " + receiptCategories.size());
		}
		return receiptCategories;
		
	}

	public ReceiptCategory getMyReceiptCategory(String id) {
		Query query = new Query(Criteria.where("id").is(id));
		return mongoTemplate.findOne(query, ReceiptCategory.class, getMyCollection(ReceiptCategory.class));
	}

	public List<ReceiptCategory> getAllMyReceiptCategories() {
		List<ReceiptCategory> receiptCategories = mongoTemplate.findAll(ReceiptCategory.class, getMyCollection(ReceiptCategory.class));
		return receiptCategories;
	}

	public ReceiptCategory getMyReceiptCategoryByName(String value) {
		ReceiptCategory receiptCategory = null;
		Query query = new Query(Criteria.where("value").is(value));
		receiptCategory = mongoTemplate.findOne(query, ReceiptCategory.class, getMyCollection(ReceiptCategory.class));
		return receiptCategory;
	}

}
